//
//  RDReader.m
//  readApp
//
//  Created by 朱献国 on 2018/8/16.
//  Copyright © 2018年 😝😝😝😝😝. All rights reserved.
//

#import "RDReader.h"
#import "RDPageViewController.h"

@interface RDReader ()
<
UIPageViewControllerDelegate,
UIPageViewControllerDataSource
>

@property (nonatomic, strong) RDPageViewController *pageViewController;

@end

@implementation RDReader

#pragma mark - Overwrite
- (void)viewDidLoad {
    [super viewDidLoad];
  
  [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pagingTaped:)]];
  
  [self loadReaderView];
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  
}

- (void)viewWillDisappear:(BOOL)animated {
  [super viewWillDisappear:animated];
  
  
}

#pragma mark - Event
- (void)pagingTaped:(UITapGestureRecognizer *)ges {
  
  // 阅读页设置 有效点击区域为 屏幕宽度的1/3
  CGPoint tapPoint = [ges locationInView:self.view];
  CGRect validRect = CGRectMake(KSCREEN_WIDTH / 3.0, 0, KSCREEN_WIDTH / 3.0, KSCREEN_HEIGHT);
  if (CGRectContainsPoint(validRect, tapPoint)) {
    if (self.delegate && [self.delegate respondsToSelector:@selector(readerSettingClicked:)]) {
      [self.delegate readerSettingClicked:self];
    }
  }
  
}

#pragma mark - CreateView
- (void)loadReaderView {
  
  // 阅读容器
  [self loadPageViewController];
  
  // 阅读背景图片
  if(self.config.readerBGImage) {
    [self loadBackgroundImage];
  }
}

- (void)loadPageViewController {
  
//  self.clearReaderViewIfNeed()
  
//  let transtionStyle: UIPageViewControllerTransitionStyle = (self.config.scrollType == .curl) ? .pageCurl : .scroll
//  self.pageVC?.isDoubleSided = (self.config.scrollType == .curl) ? true : false
  
  UIPageViewControllerTransitionStyle style = UIPageViewControllerTransitionStylePageCurl;
  
  // 创建 UIPageViewController
  RDPageViewController *pageViewController = [[RDPageViewController alloc] initWithTransitionStyle:style navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
  self.pageViewController = pageViewController;
  pageViewController.dataSource = self;
  pageViewController.delegate = self;
  pageViewController.doubleSided = YES; // 双面显示
  
  // 添加到父控制器中
  [self addChildViewController:pageViewController];
  [pageViewController didMoveToParentViewController:self];
  
  [self.view addSubview:pageViewController.view];
}

- (void)loadBackgroundImage {
  
  //        var curPage: DUAPageViewController? = nil
  //        if config.scrollType == .curl {
  //            curPage = pageVC?.viewControllers?.first as? DUAPageViewController
  //            if curPage != nil {
  //                let imageView = curPage?.view.subviews.first as! UIImageView
  //                imageView.image = self.config.backgroundImage
  //            }
  //        }
  //        if config.scrollType == .horizontal || config.scrollType == .none {
  //            curPage = translationVC?.childViewControllers.first as? DUAPageViewController
  //            if curPage != nil {
  //                let imageView = curPage?.view.subviews.first as! UIImageView
  //                imageView.image = self.config.backgroundImage
  //            }
  //        }
  
  UIView *firstView = [self.view.subviews firstObject];
  if (firstView && [firstView isKindOfClass:[UIImageView class]]) {
    ((UIImageView *)firstView).image = self.config.readerBGImage;
  }
  else {
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imageView.image = self.config.readerBGImage;
    [self.view insertSubview:imageView atIndex:0];
  }
}

@end
